#include "randomizeInPlace.h"

/* Implementation of the algo p.103 "Introduction To Algorithms, 2nd Ed"*/
int *randomizeInPlace(int res[]) { 
	//srand(time(NULL));
	//const int b = 15;
	int tmp, r;
	for(int i = 0; i<16; i++) {
		//r = (int) ( ((double) b+1) * rand() / (RAND_MAX + 1.0));
		r = (int) random_bits(4); 
		assert(r<16 && r>= 0);
		tmp = res[i];
		res[i] = res[r];
		res[r] = tmp;
	}
	return res;
}

/* Return the parity bit of an int*/
int norm(int diff, int stop)
{

	int poids = 0;
	int j = 1;
	for (int i = 0; i < stop; i++) {
		if ((diff & j) == j)
			poids = (poids + 1) % 2;
		j <<= 1;
	}
	return poids;
}

/* Return the numbers of bit = 1 of an int*/
int nbBits(int diff, int stop)
{

	int poids = 0;
	int j = 1;
	for (int i = 0; i < stop; i++) {
		if ((diff & j) == j)
			poids++;
		j <<= 1;
	}
	return poids;
}
